<template>
  <div>
<van-nav-bar
  title="黑马头条-登录"
/>
<van-form @submit="onSubmit">
  <van-field
    v-model="user.mobile"
    required
    name="mobile"
    label="手机号"
    placeholder="请输入11位手机号"
    :rules="[{ required: true, message: '请填写手机号',  pattern: /^1[3-9]\d{9}$/  }]"
  />
  <van-field
    v-model="user.code"
    required
    type="password"
    name="code"
    label="密码"
    placeholder="请输入六位密码"
    :rules="[{ required: true, message: '请填写密码' ,  pattern: /^\d{6}$/, }]"
  />
  <div style="margin: 16px;">
    <van-button
    round block type="info"
    native-type="submit"
    :disabled="isLoding"
    :loading="isLoding"
    loading-text="正在登陆ing"
    >登录</van-button>
  </div>
</van-form>

  </div>
</template>

<script>
import Notify from '@/ui/Notify'
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token.js'
import { setStorage } from '@/utils/storage'
export default {
  data () {
    return {
      user: {
        mobile: '15031574322',
        code: '246810'
      },
      isLoding: false
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      // console.log(this.user)
      this.isLoding = true
      try {
        const res = await loginAPI(this.user)
        // console.log(res)
        Notify({ type: 'success', message: '登陆成功啦!!!' })
        setToken(res.data.data.token)
        setStorage('refresh_token', res.data.data.refresh_token)
        this.$router.replace({
          path: this.$route.query.path || '/layout/home'
        })
      } catch (err) {
        Notify({ type: 'danger', message: '账号密码错误' })
      }
      this.isLoding = false
    }
  }
}
</script>

<style scoped lang ="less">
 /* .van-nav-bar {
     background: #007bff;
 } */
 /* /deep/ .van-nav-bar__title{
     color: white;
 } */
</style>
